﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Image))]
public class click : MonoBehaviour,IPointerClickHandler,IBeginDragHandler,IDragHandler,IEndDragHandler 
{
	private Dictionary<int,GameObject> m_DraggingIcons = new Dictionary<int,GameObject > ();
	private Dictionary<int,RectTransform> m_DragingPlanes = new Dictionary<int,RectTransform > ();
	public void OnBeginDrag(PointerEventData eventdata)
	{
		var canvas = gameObject.transform.parent;
		m_DraggingIcons [eventdata.pointerId] = new GameObject ("icon");
		m_DraggingIcons [eventdata.pointerId].transform.SetParent (canvas.transform, false);
		var image = m_DraggingIcons [eventdata.pointerId].AddComponent<Image> ();
		image.sprite = GetComponent<Image> ().sprite;
		m_DragingPlanes [eventdata.pointerId] = transform as RectTransform;
		setDragPos (eventdata);
	}
	void setDragPos(PointerEventData eventdata)
	{
		Vector3 globalmousepos;
		if(RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DragingPlanes[eventdata.pointerId],eventdata.position,eventdata.pressEventCamera,out globalmousepos))
		{
			m_DraggingIcons[eventdata.pointerId].GetComponent<RectTransform>().position=globalmousepos;
		}
	}
	public void OnDrag(PointerEventData eventdata)
	{
		setDragPos (eventdata);
	}
	public void OnEndDrag(PointerEventData eventdata)
	{
		Destroy (m_DraggingIcons [eventdata.pointerId]);
	}

	public void OnPointerClick(PointerEventData eventdata)
	{

		Debug.Log ("Click");

	}
}
